﻿using System;
using Castle.ActiveRecord;
using Hadlow.Jobvertizer.Common;

namespace Hadlow.Jobvertizer.Models
{
    [ActiveRecord]
    public class Job : Entity
    {
        string title;

        [Property]
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        string description;

        [Property]
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        bool featured;

        [Property]
        public bool Featured
        {
            get { return featured; }
            set { featured = value; }
        }

        Poster poster;

        [BelongsTo("PosterId")]
        public Poster Poster
        {
            get { return poster; }
            set { poster = value; }
        }

        Location location;

        [BelongsTo("LocationId")]
        public Location Location
        {
            get { return location; }
            set { location = value; }
        }

        Salary salary;

        [BelongsTo("SalaryId")]
        public Salary Salary
        {
            get { return salary; }
            set { salary = value; }
        }

        public override string ToString()
        {
            return this.title;
        }

        public bool CanBeEditedBy(User user)
        {
            // any administrator can edit a job
            if (user.Role == Role.Administrator) return true;

            // if user is not a poster or an admin, they can't edit the job
            if (user.Role != Role.Poster) return false;

            // user must be a poster. They can only edit the job if they own it
            return this.Poster.PosterUsers.Exists(pu => pu.User.Id == user.Id);
        }
    }
}
